#include "kernel.h"
#include "thread.h"
#define unsigned int u32
/*
 * as its name it is the globl task structure it include a thread's(may be i could use 
 * this word to descript ) detail. 
 * in linux task_struct is defined in the /include/linux/sched.h
 */
/*
 * struct TSS is the task stack structure it includes the most of the cpu register
 * and stack register,it should be includeed in the task_struct 
 */
/*
 * 进程的各种状况
 */
#define 	TASK_INTERRUPTIBLE 	1
#define 	TASK_UNINTERRUPTIBLE 	2
#define 	TASK_STOPPED 		3
#define 	TASK_RUNNING 		4
#define 	TASK_ZOMBIE 		5


/*
 * use tss to resp the many register
 */
struct TSS{
	u32 eax;
	u32 ebx;
	u32 ecx;
	u32 edx;
	u32 ebp;
	u32 edp;
	u32 esp;
	u32 edi;
	u32 epi;
	u16 ax;
	u16 bx;
	u16 cx;
	u16 ss;
	u16 dx;
	u32 tr;
};

/*
 * task_struct is the most complicated structure in the kernel ,i hate it
 * but i had to finish it by myself.because it is the most important
 */
struct task_struct
{
	u16 state;
	u16 blocked;
	u32 time;
	u32 close_on_exec;             	//what is this？
	struct TSS ss;   		//任务的TSS 段
	u16 uid;
	u16 gid;
	u16 pid;
	struct task_struct *next;
	struct task_struct *prev;    	//作为双链表来实现
	u32 counter;  			//时间片
	u16 priority;    		//优先级
	u32 alarm;      		//定时器
	struct thread_struct thread;   	//进程中包含的线程
	u16 tty;
	struct m_inode *pwd;     	//当前工作目录
	struct m_inode *root;     	//根目录
	struct m_inode *executable;  	//执行文件的i节点
	u16 	exit_code;  		//退出返回数值
	u32 start_code;
	u32 end_code;
	u32 end_data;
	u32 start_stack;
	u32 totle_length;
	u32 user_time; 	//用户态使用的时间
	u32 supe_time;  	//超级用户使用的时间	
	//只允许由32个信号量动作
	struct sigaction sigaction[32];
	struct tty_struct *tty;
};

/*
 * 内核中应该维护了一个线程链表
 * 应该是链表还是数组，这个很让人头疼
 */
#define BIG 128
//只允许128个进程存在
struct task_struct task_list[BIG]
struct task_struct *task_head;
struct task_struct *task_tail;

#define switch_to(nr) \
__asm__("cmpl %%ecx,current\n\t" \
	"je 1f\n\t" \
	"cli\n\t" \
	"xchgl %%ecx,current\n\t" \
	"ljmp %0\n\t" \
	"sti\n\t" \
	"1:" \
	: /* no output */ \
	:"m" (*(((char *)&tsk->ss.tr)-4)), \
	 "c" (nr) \
	:"cx")

